<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/27 0027
 * Time: 下午 8:00
 */
namespace app\common\model;

use app\admin\model\Tenantdistributiontype;
use think\Model;

class Logistics extends BaseModel{
    protected $resultSetType = 'collection';

    /**
     * @return \think\model\relation\HasOne
     * 获取商户店铺关联
     */
    public function Stores()
    {
        return $this->hasOne('store','tenantId','id');
    }

    /**
     * @return \think\model\relation\BelongsTo
     * 对应关联
     */
    public function logistics()
    {
        return $this->belongsTo('tenantdistributiontype',"t_type_id","id");
    }

    /**
     * @return \think\model\relation\HasOne
     * 获取商户店铺关联
     */
    public function Tenantdistributiontype()
    {
        return $this->hasOne('Tenantdistributiontype','t_type_id','id');
    }


    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * 获取当前商铺物流设置数据
     */
    public function getLogistics($id){
            return self::where("id","=",$id)->find();
    }


    public function tdUpdate($data,$tdata){
       $up = self::where("id","=",$data['id'])->update($data);
       $findlogs = self::where("id","=",$data['id'])->find();
        $tdt = $findlogs->Tenantdistributiontype;
        $res = $this->uType($tdata,$tdt['id']);
        return $res;
    }

    public function uType($tdata,$tid){
            return self::hasWhere('tenantdistributiontype', ['tenantdistributiontype.id'=>$tid,"tenantdistributiontype.distributionType"=>Tenantdistributiontype::D_TYPE_2])->update($tdata);
    }

    /**
     * @param $data
     * @param $tdata
     * @param $tdtTypeId
     * @return bool|int|string
     * 添加商户物流设置
     */
    public function tdAdd($data,$tdtTypeId){

        $tdtModel = new Tenantdistributiontype();
        $tdType = $tdtModel->findTdtType($tdtTypeId);
            if($tdType && !empty($tdType)){
                if($tdType->status != $data['status']){
                    $res = $tdtModel->updateStatus($tdtTypeId,$data['status']);
                    return $res;
                }else{
                    $id =  get_uniqid_id();
                    $data['id'] = $id;
                    return self::insert($data);
                }
            }

        return false;
    }

    /**
     * @param $logistics_type_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取商户配送设置信息
     */
    public function findLogistics($logistics_type_id)
    {

    return  self::where('tenantDistributionId','=',$logistics_type_id)->find();
    }

    /**
     * @param $data
     * @param $tdata
     * @param $tdtTypeId
     * @return Logistics|bool
     * 修改物流配送设置
     */
    public function updateLogistics($data,$tdtTypeId){
        $tdtModel = new Tenantdistributiontype();
        $tdtType = $tdtModel->findTdtType($tdtTypeId);

        if($tdtType && !empty($tdtType)){
            $data['updateDate'] = date("Y-m-d h:i:s",time());

             $res = self::where("tenantDistributionId",'=',$tdtTypeId)->update($data);
        return $res;
              //  echo $this->getLastSql();exit;
        }
        return false;
    }

}